package com.itlong.cloud.commons.service.impl;

import com.itlong.cloud.commons.service.IUserMovePushMessageService;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.push.message.GatewayService;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.node.JsonUtil;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <desc>
 *      用户迁入推送业务接口
 * </desc>
 *
 * @createDate 2020/07/31
 */
@Service
public class UserMovePushMessageServiceImpl implements IUserMovePushMessageService{
    private static final Logger LOG = LoggerFactory.getLogger(UserMovePushMessageServiceImpl.class);

    @Autowired
    RedisService redisService;

    private ExecutorService cachedThreadPool ; //线程池


    @PostConstruct
    public void init(){
        cachedThreadPool = Executors.newFixedThreadPool(10);
    }


    /**
     * <desc>
     *      推送数据
     * </desc>
     *
     * @param projectId
     * @author Jiaqi.X
     * @createDate 2020/07/31
     */
    @Override
    public void pushMessage(String projectId,String token) {
        cachedThreadPool.execute(()-> {
            try {
                LOG.info("项目" + projectId + "推送数据" + redisService.get(RedisConstant.PROPERTY_USER_MOVE_PERCENT_KEY + projectId));

                Map<String, Channel> map = GatewayService.getChannels();
                LOG.info("channel:" + map);
                Map<String, Object> result = new HashMap<>();
                result.put("projectId", projectId);
                result.put("token", URLEncoder.encode(token));
                result.put("pushType",2);
                while (redisService.get(RedisConstant.PROPERTY_USER_MOVE_PERCENT_KEY + projectId) != null) {
                    Iterator<String> it = map.keySet().iterator();
                    while (it.hasNext()) {
                        String key = it.next();
                        Channel obj = map.get(key);
                        result.put("percent", redisService.get(RedisConstant.PROPERTY_USER_MOVE_PERCENT_KEY + projectId));
                        System.out.println(JsonUtil.toJSON(result));
                        String strUTF8 = URLDecoder.decode(JsonUtil.toJSON(result), "UTF-8");
                        if (obj.isOpen()) {
                            obj.write(new TextWebSocketFrame(strUTF8));
                        }
                    }
                    Thread.sleep(2000);
                }
            }catch (Exception e){
                LOG.error("推送失败");
            }
        });
    }
}